//
//  KCBaseNavController.swift
//  MyKC
//
//  Created by yy on 2017/2/22.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//

import UIKit

class KCBaseNavController: UINavigationController {
    
    
    override class func initialize() {
        let navBar = UINavigationBar.appearance()
        let orcimage = UIImage(named: "导航栏")
        navBar.setBackgroundImage(orcimage, for: .default)
        navBar.tintColor = UIColor.white
        navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize: 20)]
        
        navBar.backIndicatorImage = UIImage(named: "下一页-拷贝-4")
        navBar.backIndicatorTransitionMaskImage = UIImage(named: "下一页-拷贝-4")
        
        let navItem = UIBarButtonItem.appearance()
        navItem.setBackButtonTitlePositionAdjustment(UIOffset.init(horizontal: 0, vertical: -30), for: .default)
    
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let navBar = UINavigationBar.appearance()
        navBar.setBackgroundImage(UIImage(named: "导航栏"), for: .default)
        navBar.tintColor = UIColor.white
        navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize: 20)]
        
        navBar.backIndicatorImage = UIImage(named: "下一页-拷贝-4")
        navBar.backIndicatorTransitionMaskImage = UIImage(named: "下一页-拷贝-4")
        
        let navItem = UIBarButtonItem.appearance()
        navItem.setBackButtonTitlePositionAdjustment(UIOffset.init(horizontal: 0, vertical: -64), for: .default)
//        if #available(iOS 9.0, *) {
//            UIBarButtonItem.appearance(whenContainedInInstancesOf: <#T##[UIAppearanceContainer.Type]#>)
//        } else {
//            // Fallback on earlier versions
//        }
//        UIBarButtonItem.appearance(whenContainedInInstancesOf: UISearchBar.self)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    //重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //一旦空实现这个方法 导航视图控制器无法设置根视图控制器
        //UINavigationController(rootViewController:) 内部执行了push
        print(childViewControllers.count)
        let count = childViewControllers.count
        if count > 0 {
//            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(back))
            //当进入子页面的时候 隐藏底部的tabbar
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
        print(childViewControllers.count)
    }
    
    //返回
    @objc private func back() {
        //pop
        self.popViewController(animated: true)
    }

}
